package com.liu.controller;


import com.liu.bean.User;
import com.liu.bean.params.UserQuery;
import com.liu.common.bean.PageBean;
import com.liu.common.bean.ResultBean;
import com.liu.common.controller.BaseController;
import com.liu.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api")
@Api(tags = "用户管理")
public class UserController extends BaseController {

    @Autowired
    UserService userService;


    @ApiOperation("查询用户")
    @PostMapping("/user/list")
    public ResultBean<PageBean<User>> query(@RequestBody UserQuery userQuery) {
        PageBean bean = userService.query(userQuery.toMap());
        return success("用户列表",bean);
    }


    @ApiOperation("保存用户")
    @PostMapping("/user")
    public ResultBean save(@Validated @RequestBody User user, BindingResult result){
        validate(result);
        int rs = userService.sava(user);
        return rs==1?success("保存成功"):fail("保存失败");
    }

    @ApiImplicitParam(name = "id",value = "用户id",defaultValue = "",required = true,dataTypeClass = String.class,paramType = "query")
    @ApiOperation("删除用户")
    @DeleteMapping("/user")
    public ResultBean delete(@RequestParam(name = "id",defaultValue = "",required = true) String id){
        return success("删除成功");
    }



}
